#include "option.h"
#include "ui_option.h"
#include <QDebug>
#include "gamemanager.h"
Option::Option(QWidget *parent) :
    MyWidget(parent),
    ui(new Ui::Option)
{
    ui->setupUi(this);
    Init();
}

Option::~Option()
{
    delete ui;
}

void Option::Init()
{
    btns.push_back(ui->OptionBtn0);
    btns.push_back(ui->OptionBtn1);
    btns.push_back(ui->OptionBtn2);
    btns.push_back(ui->OptionBtn3);
    btns.push_back(ui->OptionBtn4);
    for(int i = 0;i < btns.count();i++)
    {
        btns[i]->info = UILib::Instance().btns["选项"];
        btns[i]->SetSelectedDisabled(true);
        btns[i]->Refresh(UILib::Instance().btns["选项"]);

        //对应的linker
        linkers.push_back(nullptr);
        //点击绑定linker
        connect(btns[i],&QPushButton::clicked,this,[=](){
            qDebug()<<__FILE__<<__LINE__<<"点击";
            if(!linkers[i])
                return;
            bool conditions = linkers[i]->AllConditionSatisfy();
            if(conditions)
            {
                qDebug()<<"条件全部满足";
                linkers[i]->HandleAllAction();
                int b = linkers[i]->branchId;
                int c = linkers[i]->cycleId;

                int nextId = -1;
                if(NarrationTree::Instance().narrationTree[b])
                {
                    //emit EventCenter::Instance().RunNarration(b);
                    nextId = b;
                }
                else if(NarrationTree::Instance().narrationTree[c])
                {
                    //emit EventCenter::Instance().RunNarration(c);
                    nextId = c;
                }
                else
                {
                    emit EventCenter::Instance().NextDialog(true);
                    return;
                }

                NarrationNode* curNode = GameManager::Instance().curNode;
                if(curNode && (nextId == b || nextId == c))
                {
                    int msecond = curNode->blackTime;
                    if(msecond <= 0 || !curNode->black)
                    {
                        if(NarrationTree::Instance().narrationTree[nextId])
                            emit EventCenter::Instance().RunNarration(nextId);
                        return;
                    }
                    emit EventCenter::Instance().Black(msecond);
                    QTimer::singleShot(msecond,[=](){
                        if(NarrationTree::Instance().narrationTree[nextId])
                            emit EventCenter::Instance().RunNarration(nextId);
                    });
                }
            }
        });
    }
}

void Option::Refresh(int begin)
{
    begin = qMin(btns.count(),begin);
    for(int i = 0;i < begin;i++)
    {
        btns.at(i)->show();
    }
    for(int i = begin;i < btns.count();i++)
    {
        btns.at(i)->hide();
    }
}

void Option::ChangeBtnText(QString text, int index)
{
    if(index < 0 || index >= btns.count())
        return;
    btns[index]->setText(text);
}

void Option::ChangeLinker(Linker *linker, int index)
{
    linkers[index] = linker;
}
